package cn.itrip.service.Impl;

import cn.itrip.DTO.ItripListHotelOrderDTO;
import cn.itrip.DTO.ItripPersonalOrderRoomDTO;
import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripHotelRoomMapper;
import cn.itrip.dao.ItripOrderLinkUserMapper;
import cn.itrip.entity.ItripHotelOrder;
import cn.itrip.entity.ItripOrderLinkUser;
import cn.itrip.entity.ItripUserLinkUser;
import cn.itrip.service.ItripHotelOrderService;
import cn.itrip.utils.BigDecimalUtil;
import cn.itrip.utils.Constants;
import cn.itrip.utils.EmptyUtils;
import cn.itrip.utils.Page;
import cn.itrip.vo.biz.ItripSearchOrderVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.math.BigDecimal.ROUND_DOWN;

@Service
public class ItripHotelOrderServiceImpl extends ServiceImpl<ItripHotelOrderMapper,ItripHotelOrder> implements ItripHotelOrderService {

    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripOrderLinkUserMapper itripOrderLinkUserMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Override
    public ItripHotelOrder getItripHotelOrderByOrderNo(String orderNo) {
        return baseMapper.getItripHotelOrderByOrderNo(orderNo);
    }

    @Override
    public Double getOrderPayAmount(int count, Long roomId) throws Exception {
        BigDecimal payAmount = null;
        BigDecimal roomPrice = BigDecimal.valueOf(itripHotelRoomMapper.getItripHotelRoomById(roomId).getRoomPrice());
        payAmount = BigDecimalUtil.OperationASMD(count, roomPrice,
                BigDecimalUtil.BigDecimalOprations.multiply,
                2, ROUND_DOWN);
        Double d = payAmount.doubleValue();
        return d;
    }

    @Override
    public Map<String, String> itriptxAddItripHotelOrder(ItripHotelOrder itripHotelOrder, List<ItripUserLinkUser> linkUserList) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        if (null != itripHotelOrder) {
            int flag = 0;
            if (EmptyUtils.isNotEmpty(itripHotelOrder.getId())) {
                //删除联系人
                itripOrderLinkUserMapper.deleteItripOrderLinkUserByOrderId(itripHotelOrder.getId());
                itripHotelOrder.setModifyDate(new Date());
                flag = itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
            } else {
                itripHotelOrder.setCreationDate(new Date());
                flag = itripHotelOrderMapper.insertItripHotelOrder(itripHotelOrder);
            }
            if (flag > 0) {
                Long orderId = itripHotelOrder.getId();
                //添加订单之后还需要往订单与常用联系人关联表中添加记录
                if (orderId > 0) {
                    for (ItripUserLinkUser itripUserLinkUser : linkUserList) {
                        ItripOrderLinkUser itripOrderLinkUser = new ItripOrderLinkUser();
                        itripOrderLinkUser.setOrderId(orderId);
                        itripOrderLinkUser.setLinkUserId(itripUserLinkUser.getId());
                        itripOrderLinkUser.setLinkUserName(itripUserLinkUser.getLinkUserName());
                        itripOrderLinkUser.setCreationDate(new Date());
                        itripOrderLinkUser.setCreatedBy(itripHotelOrder.getCreatedBy());
                        itripOrderLinkUserMapper.insertItripOrderLinkUser(itripOrderLinkUser);
                    }
                }
                map.put("id", itripHotelOrder.getId().toString());
                map.put("orderNo", itripHotelOrder.getOrderNo());
                return map;
            }
        }
        return map;
    }

    @Override
    public ItripHotelOrder getItripHotelOrderById(Long id) throws Exception {
        return baseMapper.getItripHotelOrderById(id);
    }

    @Override
    public Page<ItripListHotelOrderDTO> queryOrderPageByMap(ItripSearchOrderVO itripSearchOrderVO) throws Exception {
        Integer total = baseMapper.getOrderCountByMap(itripSearchOrderVO);
        Page page = new Page(itripSearchOrderVO.getPageNo(), itripSearchOrderVO.getPageSize(), total);
        List<ItripListHotelOrderDTO> itripHotelOrderList = baseMapper.getOrderListByMap(itripSearchOrderVO);
        page.setRows(itripHotelOrderList);
        return page;
    }

    @Override
    public ItripPersonalOrderRoomDTO getItripHotelOrderRoomInfoById(Long orderId) throws Exception {
        return baseMapper.getItripHotelOrderRoomInfoById(orderId);
    }
}
